package com.ddyx.service.user.app.system.controller;

import com.alibaba.fastjson.JSONObject;
import com.ddyx.common.base.controller.BaseController;
import com.ddyx.common.base.enums.ApiResultType;
import com.ddyx.common.framework.aop.annotation.LoginRequired;
import com.ddyx.common.utils.io.StringUtil;
import com.ddyx.service.user.api.entity.User;
import com.ddyx.service.user.api.vo.UserVo;
import com.ddyx.service.user.app.system.service.IUserService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * <p>
 *      用户管理模块
 * </p>
 * class: UserController.java
 * author: agula
 * since: 2019-07-11  13:42
 */
@Validated
@Api(value = "用户管理模块", tags = "用户管理")
@RestController
@RequestMapping("/user")
public class UserController extends BaseController {

    @Resource
    IUserService userService;

    @ApiOperation(value="用户添加接口", notes="根据用户对象创建数据,并生成user账号。")
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public Object addUser(@Valid @RequestBody UserVo userVo) {
        userService.addUser(userVo);
        return successResult();
    }

    @ApiOperation(value="用户列表接口", notes="分页查询用户列表接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "分页页码", required = true, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "size", value = "每页条数", required = true, dataType = "int", paramType = "query")
    })
    @RequestMapping(value = "/getList", method = RequestMethod.GET)
    public Object getList(@NotNull @RequestParam Integer page, @NotNull @RequestParam Integer size) {

        Page<Object> pageHelper = PageHelper.startPage(page, size);
        List<User> userList = userService.getUserList();
        int total = (int) pageHelper.getTotal();
        return successResult(userList, total);
    }

    @ApiOperation(value="根据手机号查询用户信息", notes="根据手机号查询用户信息")
    @ApiImplicitParam(name = "phone", value = "用户手机号码", required = true, dataType = "String", paramType = "query", defaultValue = "18515990924")
    @RequestMapping(value = "/getUserByPhone", method = RequestMethod.POST)
    public Object getUserByPhone(@NotNull @RequestParam String phone) {
        User user = userService.getUserByPhone(phone);
        return successResult(user);
    }

    @ApiOperation(value="用户登录接口", notes="用户手机号、验证码登录。")
    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public Object login(@Valid @RequestBody UserVo userVo, HttpServletRequest request) {
        JSONObject param = this.getParameter(request);
        String phone = param.getString("phone");
        String verifyCode = param.getString("verifyCode");

        if (StringUtil.isEmpty(phone, verifyCode)) {
            return errorResult(ApiResultType.PARAMER_ERROR);
        }

        User user = userService.login(phone, verifyCode);
        if (user == null) return errorResult(ApiResultType.REQUEST_FAILED);
        if (user.getStatus() == 1 || user.getStatus() == 2 || user.getStatus() == 3)
            return errorResult(ApiResultType.USER_DISABLE);

        return successResult(user);
    }

    @ApiOperation(value="用户完善资料接口", notes="用户完善资料")
    //@LoginRequired
    @ApiImplicitParam(name = "token", value = "token", required = true, dataType = "String", paramType = "header")
    @RequestMapping(value = "/updateUserInfo/{userId}", method = RequestMethod.POST)
    public Object updateUserInfo(@NotNull @PathVariable int userId, @Valid @RequestBody UserVo userVo, HttpServletRequest request) {
//        User userSession = getUserSession(request);
//        if (!userSession.getId().equals(userId)) {
//            return errorResult(ApiResultType.PARAMER_ERROR);
//        }
        int rs = userService.updateUser(userVo);
        if (rs < 1) {
            return errorResult(ApiResultType.REQUEST_FAILED);
        }
        return successResult(userService.getUserInfo(userId));
    }

    //@LoginRequired
    @ApiImplicitParam(name = "token", value = "token", required = true, dataType = "String", paramType = "header")
    @ApiOperation(value="获取用户资料接口", notes="根据用户token获取指定用户资料")
    @RequestMapping(value = "/getUserInfo/{userId}", method = RequestMethod.GET)
    public Object getUserInfo(@NotNull @PathVariable int userId) {

        User user = userService.getUserInfo(userId);
        if (user == null) {
            return errorResult(ApiResultType.REQUEST_FAILED);
        }
        return successResult(user);
    }


    @ApiOperation(value="获取用户资料及最新动态接口-用户卡片", notes="根据用户token获取指定用户资料及最新动态")
    @RequestMapping(value = "/getUserAndLiving/{fromUserId}/{toUserId}", method = RequestMethod.GET)
    public Object getUserAndLiving(@NotNull @PathVariable int fromUserId, @NotNull @PathVariable int toUserId) {

        return successResult(userService.getUserAndLiving(fromUserId, toUserId));
    }


    @LoginRequired
    @ApiOperation(value="用户登出接口", notes="用户登出")
    @ApiImplicitParam(name = "token", value = "token", required = true, dataType = "String", paramType = "header")
    @RequestMapping(value = "/logout", method = RequestMethod.GET)
    public Object logout(HttpServletRequest request) {
//        User userSession = getUserSession(request);
//        userService.logout(userSession);
        return successResult();
    }


    @GetMapping("/user-info-by-id")
    public Object userInfo(Integer userId) {

        User userInfo = userService.getUserInfo(userId);

        return successResult(userInfo);
    }


    @GetMapping("/user-info")
    public Object userInfo(String account, String password) {
        User userInfo = userService.getUserInfo(0);
        return successResult(userInfo);
    }

}
